package edu.yngsxy.communityservice.core.ai;

import cn.hutool.ai.core.Message;

import edu.yngsxy.communityservice.core.ai.service.AISessionService;
import edu.yngsxy.communityservice.core.model.common.Result;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Set;

/**
 * AI大模型-会话管理
 */
@RestController
@RequestMapping("/api/v1/ai/session")
@Tag(name = "AI大模型") // 分组标签
@Slf4j
public class AISessionController {
    @Autowired
    AISessionService aiSessionService;

    /**
     * 获取指定会话的历史记录
     */
    @GetMapping("/history/{sessionId}")
    @Operation(summary = "获取会话历史", description = "根据会话ID获取历史对话记录")
    public Result<List<Message>> getChatHistory(
            @Parameter(description = "会话ID", required = true)
            @PathVariable String sessionId) {
        try {
            List<Message> messages = aiSessionService.getHistory(sessionId);
            return Result.success(messages);
        } catch (Exception e) {
            log.error("获取会话历史失败: {}", e.getMessage(), e);
            return Result.error("获取历史记录失败");
        }
    }

    /**
     * 清除指定会话的历史记录
     */
    @DeleteMapping("/history/{sessionId}")
    @Operation(summary = "清除会话历史", description = "根据会话ID清除历史对话记录")
    public Result<Object> clearChatHistory(
            @Parameter(description = "会话ID", required = true)
            @PathVariable String sessionId) {
        try {
            aiSessionService.clearHistory(sessionId);
            return Result.success(null);
        } catch (Exception e) {
            log.error("清除会话历史失败: {}", e.getMessage(), e);
            return Result.error("清除历史记录失败");
        }
    }

    /**
     * 获取用户的所有会话记录
     * @param userId 用户ID
     * @return 用户会话记录
     */
    @GetMapping("/user/{userId}")
    @Operation(summary = "获取用户会话", description = "根据用户ID获取所有会话记录")
    public Result<Set<String>> getUserChatHistory(
            @Parameter(description = "用户ID", required = true)
            @PathVariable String userId) {
        try {
            Set<String> sessionMap = aiSessionService.getAllSession(userId);
            return Result.success(sessionMap);
        } catch (Exception e) {
            log.error("获取用户会话失败: {}", e.getMessage(), e);
            return Result.error("获取用户会话记录失败");
        }
    }

    /**
     * 清除指定用户的会话记录
     * @param userId 用户ID
     * @return 清除结果
     */
    @DeleteMapping("/user/{userId}")
    @Operation(summary = "清除用户会话", description = "根据用户ID清除所有会话记录")
    public Result<Object> clearUserChatHistory(
            @Parameter(description = "用户ID", required = true)
            @PathVariable String userId) {
        try {
            aiSessionService.clearAllSession(userId);
            return Result.success(null);
        } catch (Exception e) {
            log.error("清除用户会话失败: {}", e.getMessage(), e);
            return Result.error("清除会话记录失败");
        }
    }

    /**
     * 获取所有会话记录
     * @return 所有会话记录
     */
    @GetMapping("/all")
    @Operation(summary = "获取所有会话", description = "获取所有会话记录")
    public Result<Set<String>> getAllChatHistory() {
        try {
            Set<String> sessionMap = aiSessionService.getAllSession();
            return Result.success(sessionMap);
        } catch (Exception e) {
            log.error("获取所有会话失败: {}", e.getMessage(), e);
            return Result.error("获取所有会话记录失败");
        }
    }

    /**
     * 清除所有会话记录
     * @return 清除结果
     */
    @DeleteMapping("/all")
    @Operation(summary = "清除所有会话", description = "清除所有会话记录")
    public Result<Object> clearAllChatHistory() {
        try {
            aiSessionService.clearAllSession();
            return Result.success(null);
        } catch (Exception e) {
            log.error("清除所有会话失败: {}", e.getMessage(), e);
            return Result.error("清除所有会话记录失败");
        }
    }
}

